import 'dart:convert';

import 'package:spring_fairy/model/user.dart';
import 'api.dart';

class UserAPI extends API {
  Future<User?> register(String username,String password,String email) async {
    try {
      var response = await sendJSONPostRequest("/user/register", {
        "username":username,
        "password":password,
        "email":email
      });
      return User.fromJson(json.decode(response));
    } catch(err) {
      return null;
    }
  }

  Future<User?> login(String username,String password) async {
    try {
      var response = await sendJSONPostRequest("/user/login", {
        "username":username,
        "password":password
      });
      return User.fromJson(json.decode(response));
    } catch(err) {
      print(err);
      print("=========================");
      return null;
    }
  }

  Future<User?> info() async {
    if(API.token == null) {
      return null;
    }
    var response = await sendGETRequest("/user/info");
    if(response == null) {
      return null;
    }
    try {
      return User.fromJson(json.decode(response));
    } catch(_) {
      return null;
    }
  }

}

